{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
</head>
<body>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <h3 class="text-center">修改密码</h3>
            <h5 class="text-center">用户:{{ request.user }}</h5>
            <form method="post" onsubmit="return false;" class="form-horizontal" id="update_form">
                <div class="form-group">
                    <label for="old_password" class="control-label col-md-3">
                        旧密码
                    </label>
                    <div class="col-md-9">
                        <input type="password" class="form-control" name="old_password">
                    </div>
                </div>
                <div class="form-group">
                    <label for="new_password" class="control-label col-md-3">
                        新密码
                    </label>
                    <div class="col-md-9">
                        <input type="password" class="form-control" name="new_password">
                    </div>
                </div>
                <div class="form-group">
                    <label for="new_password" class="control-label col-md-3">
                        新密码确认
                    </label>
                    <div class="col-md-9">
                        <input type="password" class="form-control" name="new_password2">
                    </div>
                </div>
                {# csrftoken校验！ #}
                {% csrf_token %}
                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-9">
                        <span id="err_info" class="text-danger"></span>
                        <button class="btn btn-success pull-right" id="update_btn">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<script src="{% static 'js/jquery-3.4.1.js' %}"></script>
<script src="{% static 'js/jquery.cookie.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
<script>

    $('#update_btn').click(
        function () {
            var formdata = new FormData($('#update_form')[0]);
            $.ajax({
                url:{% url 'update_pwd' %},
                type:"post",
                data:formdata,
                processData:false,
                contentType:false,
                success:function (ret) {
                    if (ret['update_status']){
                        console.log(ret);
                        $('#err_info').text("修改成功！");
                        location.href = ret["url"];
                    }else{
                        if (ret['err_code']===20){
                            $('#err_info').text("旧密码错误！");
                        }else if(ret['err_code']===21){
                            $('#err_info').text("两次密码不一致！");
                        }else if(ret['err_code']===0){
                            $('#err_info').text("新密码不能为空！");
                        }
                    }
                }
            });
        }
    );

</script>
</body>
</html>